#include "Material.h"

#include "Texture.h"
#include "utilities.h"
#include <GL/gl.h>


Material::Material() :
    m_texture(NULL), Diffuse(Vector3f(0.5, 0.5, 0.5)), Specular(0.5, 0.5, 0.5), Shininess(10)
{
}

void Material::setDiffuse(float r, float g, float b)
{
    Diffuse[0] = r;
    Diffuse[1] = g;
    Diffuse[2] = b;
}

void Material::setSpecular(float r, float g, float b)
{
    Specular[0] = r;
    Specular[1] = g;
    Specular[2] = b;
}

void Material::setShininess(float s)
{
    Shininess = s;
}

Vector3f Material::getDiffuse()
{
    return Diffuse;
}

Vector3f Material::getDiffuse(float u, float v)
{
    if(m_texture)
        return m_texture->getColor(u, v) * Diffuse;

    return Diffuse;
}

Vector3f Material::getSpecular()
{
    return Specular;
}

float Material::getShininess()
{
    return Shininess;
}

void Material::applyGL()
{
    GLfloat dif[4]={Diffuse.x,Diffuse.y,Diffuse.z,1};
    GLfloat spec[4]={Specular.x,Specular.y,Specular.z,1};

    glMaterialfv(GL_FRONT, GL_DIFFUSE,dif);
    glMaterialfv(GL_FRONT, GL_SPECULAR,spec);
    glMaterialf(GL_FRONT, GL_SHININESS,this->Shininess);

    if(m_texture)
        m_texture->bind();
    else
        glDisable(GL_TEXTURE_2D);
}

void Material::setTexture(Texture *texture)
{
    m_texture = texture;
    m_texture->load();
}
